export default function ({
    store,
    redirect,
    $axios,
    app,
    route
}) {
    function checkUser() {
        var p = new Promise(function (resolve, reject) {
            const token = app.$cookies.get("token");
            if (!token) {
                return redirect("/login");
            } else if (!store.state.userInfo) {
                $axios.$post("/new/userInfo").then(resUser => {
                    const resultUser = resUser.data;
                    if (resUser && resultUser) {
                        store.commit('authentication/setUserInfo', resultUser)
                        store.commit('authentication/setToken', token)
                    } else {
                        redirect("/login");
                    }
                    // 返回传递用户信息
                    resolve(resultUser)
                });
            }
        });
        return p;
    };

    function getPermision(data) {
        var p = new Promise(function (resolve, reject) {
            if (store.state.PermisionsStorage.HasPermision.routers.length == 0) {
                let routers = []
                let widgets = []
                $axios.$get("/new/systemctl/getpermisions").then(res => {
                    const permisions = res.data;
                    if (res && permisions) {
                        if (permisions.routers && permisions.routers.length > 0) {
                            routers = permisions.routers;
                        }
                        if (permisions.widgets && permisions.widgets.length > 0) {
                            widgets = permisions.widgets;
                        }
                        store.commit("PermisionsStorage/setHasPermision", {
                            routers: routers,
                            widgets: widgets,
                        });

                        // 返回新获取的权限列表
                        resolve({
                            routers:routers,
                            widgets:widgets
                        })
                    }
                });
            }
            else{
                // 返回状态管理中的权限
                resolve(store.state.PermisionsStorage.HasPermision)
            }
        });
        return p;
    };

    function checkPermisioin(data) {
        var p = new Promise(function (resolve, reject) {
            if(data.routers && data.routers.length>0){
                
                // 判断当前路由是否在权限列表存在、如果存在返回true 、不存在则返回false
                if(data.routers.indexOf(route.name) < 0){
                    resolve(false)
                }
                else{
                    resolve(true)
                }
            }
            else{
                resolve(false)
            }
        });
        return p;
    };

    checkUser()
        .then(function (data) {
            // 获取权限列表
            return getPermision(data);
        })
        .then(function (data) {
            // 验证是否有权限
            return checkPermisioin(data);
        })
        .then(function (data) {
            // 如果没有权限的话跳转到”没有权限“页面
            if(data === false){
                // redirect("/nopermision");
            }
        });
}
